import config from "../../utils/config.js";
// pages/CarDetailOld/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ConsultationFlag: 'none', //咨询模态框参数，block显示none隐藏
    goodsInfo: '',//商品详情
    images: [],//轮播图片数组
    imagestotal: '',//轮播图总数
    imagesnow: 1,//当前轮播图索引
    carMethod: [],//车辆信息
    platExplain: [],//富文本列表,[0]购车须知，[1]其他说明
    paramList: [],//车辆配置
    nearShop: [],//门店列表
    nearShopSel: {},//当前选择门店
    storepage: 1,//当前门店列表页码
    storepagesize: 6,//门店列表页面记录数,
    storepagetotal: 1,//门店列表总页数
    storekeyword: '',//门店关键字
    storetitle: "上拉加载更多",//门店列表底部显示文字
    markers: {},//当前门店的标记点信息
    storeflag: false,//门店列表显示隐藏标识
    longitude: '',//经度
    latitude: '',//纬度
    phone: '',//当前手机号
    content:'',//车型亮点富文本
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    const id = options.goodsid;
    config.posthttp('api/v1/getGoodsInfo', { goods_id: id }, (res) => {
      let data = res.data.platExplain;
      for (let index in data) {
        data[index].content.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:0 auto;" ')
      }
      that.setData({
        goodsInfo: res.data.goodsInfo,
        content: res.data.goodsInfo.goods_content.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:0 auto;" '),
        images: res.data.totalImage,
        imagestotal: res.data.totalImage.length,
        carMethod: res.data.carMethod[0],
        platExplain: data,
        paramList: res.data.paramList[res.data.paramList.length-1].param_list
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const that = this;
    //定位获取附近门店
    wx.getLocation({
      type: 'gcj02', 
      complete: function (res) {
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude
        })
        config.posthttp('api/v1/getNearShop', { longitude: that.data.longitude, latitude: that.data.latitude, page: that.data.storepage, page_size: that.data.storepagesize, keyword: that.data.storekeyword }, (res) => {
          that.setData({
            nearShop: res.data.nearShop,
            nearShopSel: res.data.nearShop[0],
            storepagetotal: res.data.total_page,
            storepage: that.data.storepage * 1 + 1,
            markers: [{
              latitude: res.data.nearShop[0].latitude,
              longitude: res.data.nearShop[0].longitude,
              title: res.data.nearShop[0].shop_name
            }],
          })
          if (that.data.storepage > res.data.total_page) {
            that.setData({
              storetitle: "没有更多数据"
            })
          }
        })

      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      phone: wx.getStorageSync('phone')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const that = this;
    return {
      title: `向您推荐这款车型`,
      path: `/pages/CarDetailOld/index?goodsid=${that.data.goodsInfo.goods_id}`,
    }
  },

  //咨询模态框显示
  Consultation: function () {
    const that = this;
    that.setData({
      ConsultationFlag: 'block'
    })
  },

  //咨询模态框隐藏
  ConsultationClose: function () {
    const that = this;
    that.setData({
      ConsultationFlag: 'none'
    })
  },

  //前往图片详情
  toimage: function () {
    const that = this;
    wx.navigateTo({
      url: '/pages/CarDetail/image?goodsid=' + that.data.goodsInfo.goods_id,
    })
  },

  //轮播滑动事件
  swiperChange(e) {
    const imagesnow = (e.detail.current) * 1 + 1;
    this.setData({
      imagesnow: imagesnow
    })
  },

  //选择门店
  selectstore: function (e) {
    const that = this;
    const index = e.currentTarget.dataset.index;
    that.setData({
      nearShopSel: that.data.nearShop[index],
      storeflag: false,
      markers: [{
        latitude: that.data.nearShop[index].latitude,
        longitude: that.data.nearShop[index].longitude,
        title: that.data.nearShop[index].shop_name
      }],
    })
  },

  //门店列表打开关闭
  storeshowclose: function () {
    const that = this;
    if (that.data.storeflag) {
      that.setData({
        storeflag: false
      })
    } else {
      that.setData({
        storeflag: true
      })
    }
  },

  //上拉加载获取门店列表
  getNearShop: function () {
    const that = this;
    if (that.data.storepage <= that.data.storepagetotal) {
      config.posthttp('api/v1/getNearShop', { longitude: that.data.longitude, latitude: that.data.latitude, page: that.data.storepage, page_size: that.data.storepagesize, keyword: that.data.storekeyword }, (res) => {
        that.setData({
          nearShop: that.data.nearShop.concat(res.data.nearShop),
          storetitle: "上拉加载更多"
        })
      })
    } else {
      that.setData({
        storetitle: "没有更多数据"
      })
    }
    that.setData({
      storepage: that.data.storepage * 1 + 1
    })

  },

  //表单搜索获取门店列表
  getNearShopsearch(e) {
    const that = this;
    that.setData({
      storekeyword: e.detail.value.replace(/\s+/g, ''),
      storepage: 1
    })
    config.posthttp('api/v1/getNearShop', { longitude: that.data.longitude, latitude: that.data.latitude, page: that.data.storepage, page_size: that.data.storepagesize, keyword: that.data.storekeyword }, (res) => {
      that.setData({
        nearShop: res.data.nearShop,
        storetitle: "上拉加载更多",
        storepage: that.data.storepage * 1 + 1,
        storepagetotal: res.data.total_page
      })
      if (that.data.storepage > res.data.total_page) {
        that.setData({
          storetitle: "没有更多数据"
        })
      }
    })
  },

  //前往地图
  tomap: function () {
    const that = this;
    wx.navigateTo({
      url: '/pages/map/index?nearShopSel=' + JSON.stringify(that.data.nearShopSel),
    })
  },

  //表单修改手机号码
  updatephone: function (e) {
    const that = this;
    const data = e.detail.value;
    that.setData({
      phone: data,
    })
  },

  //预约咨询接口
  makeDatement: function () {
    const that = this;
    if ((/^1\d{10}$/.test(that.data.phone))) {
      config.posthttp('api/v1/makeDatement', { goods_id: that.data.goodsInfo.goods_id, shop_id: that.data.nearShopSel.id, mobile: that.data.phone, cate_id:0 }, (res) => {
        wx.showToast({
          title: '提交成功',
          icon: 'success'
        })
        setTimeout(() => {
          that.setData({
            ConsultationFlag: 'none',
          })
        }, 1500)
      })
    } else {
      wx.showToast({
        title: '手机号码有误',
        icon: "none"
      })
    }
  },

  //当前门店咨询
  nowstoreConsultation: function () {
    const that = this;
    that.setData({
      storeflag: false,
      ConsultationFlag: 'block',
    })
  },

  //其他门店咨询
  otherstoreConsultation: function (e) {
    const that = this;
    const index = e.currentTarget.dataset.index;
    that.setData({
      nearShopSel: that.data.nearShop[index],
      storeflag: false,
      ConsultationFlag: 'block',
      markers: [{
        latitude: that.data.nearShop[index].latitude,
        longitude: that.data.nearShop[index].longitude,
        title: that.data.nearShop[index].shop_name
      }],
    })
  },

  //当前门店点击导航
  tonavigation: function () {
    const that = this;
    wx.openLocation({
      latitude: that.data.nearShopSel.latitude * 1,
      longitude: that.data.nearShopSel.longitude * 1,
      name: that.data.nearShopSel.shop_name,
      address: that.data.nearShopSel.address
    })
  }
})